import datetime
import random

from app import db,redis_game
from flask_sqlalchemy import BaseQuery
from redis import Redis

#redis_game = Redis(db=3)
class GameQuery(BaseQuery):

    def get_date_range_data(self,date_range):
        return None

class GameGame(db.Model):
    """
    游戏信息
    """
    __tablename__ = 'game_game'

    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    name = db.Column(db.String(20), nullable=False)
    category_list = db.Column(db.ARRAY(db.String(length=10)),nullable=False)  # 游戏分类列表
    status = db.Column(db.Integer, nullable=False,default=1)      # 游戏状态
    priority = db.Column(db.Integer, nullable=False, default=0)  # 权重
    insert_time = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now)
    game_key = db.Column(db.ARRAY(db.String(length=10)),nullable=False)  # 游戏关键词

    query_class = GameQuery
    @property
    def user_nums(self):
        return int(redis_game.hget('%s_total' %self.id,'login_nums'))
        # return UserGame.query.filter_by(game_id=self.id).count()

    @property
    def reg_nums(self):
        return int(redis_game.hget('%s_total' % self.id, 'reg_nums'))
        # return UserGame.query.with_entities(UserGame.user_id).filter_by(game_id=self.id).distinct().count()

    @property
    def pay_nums(self):
        return int(redis_game.hget('%s_total' % self.id, 'pay_nums'))

    @property
    def total_charge(self):
        return int(redis_game.hget('%s_total' % self.id, 'total'))

    @property
    def rank_point(self):
        return 0.2*self.user_nums+0.2*self.reg_nums+0.2*self.pay_nums+0.4*self.total_charge

    @property
    def all_data(self):
        return [self.user_nums,self.reg_nums,self.pay_nums,self.total_charge]

    @property
    def common_similarity(self):
        return list(map(lambda x:eval(x.decode('utf-8')),redis_game.lrange('%s_common_similarity'%self.id,0,-1)))

    @property
    def cos_similarity(self):
        return list(map(lambda x:eval(x.decode('utf-8')),redis_game.lrange('%s_cos_similarity' % self.id,0,-1)))

    @property
    def cos_alpha_similarity(self):
        return list(map(lambda x:eval(x.decode('utf-8')),redis_game.lrange('%s_cos_alpha_similarity' % self.id,0,-1)))

    @property
    def game_tag(self):
        tag = ''
        for category in self.category_list:
            tag += category+','
        return tag[:-1]

class UserGame(db.Model):
    """
    用户游戏信息表
    """
    __tablename__ = 'user_game'
    id = db.Column(db.Integer,autoincrement=True,primary_key=True)
    user_id = db.Column(db.Integer,nullable=False)
    game_id = db.Column(db.Integer)
    insert_time = db.Column(db.DateTime,default=datetime.datetime.now)
    update_time = db.Column(db.DateTime, default=datetime.datetime.now)
    
    @property
    def game_name(self):
        return GameGame.query.get(self.game_id).name

    @property
    def role_name(self):
        return 'sw_'+str(self.user_id)+str(self.game_id)

    @property
    def level(self):
        return random.randint(1,100)

    @property
    def play_time(self):
        return str(round(random.uniform(0.1,10),1))+'h'









